package com.smartcruise.util.network.customerObserver;

import com.smartcruise.R;
import com.smartcruise.base.BaseNormalView;
import com.smartcruise.util.customView.LoadingDialog;

import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

/**
 * Created by FengChaoQun
 * on 2018/2/3 0003
 * 带加载框的Observer
 * 1.在监听开始的时候显示加载框
 * 2.在监听结束的时候隐藏加载框
 * 3.在加载框被取消的时候，丢弃监听结果
 */

public abstract class BaseProgressObserver<T> implements Observer<T> {

    protected Disposable disposable;
    protected BaseNormalView baseNormalView;
    protected String loadingText;

    public BaseProgressObserver(@NonNull BaseNormalView baseNormalView) {
        this.baseNormalView = baseNormalView;
        //加载框被取消的时候 丢弃返回结果
        baseNormalView.setOnLoadingDialogDismiss(new LoadingDialog.onDismiss() {
            @Override
            public void doBeforeDismiss() {
                if (disposable != null) {
                    disposable.dispose();
                }
            }
        });
        loadingText = baseNormalView.getActivity().getString(R.string.pleaseWait);
    }

    @Override
    public void onSubscribe(Disposable disposable) {
        this.disposable = disposable;
        //开始观察的时候 显示加载框
        baseNormalView.showLoadingDialog(loadingText);
    }

    @Override
    public void onError(Throwable throwable) {
        baseNormalView.hideLoadingDialog();
    }

    @Override
    public void onComplete() {
        baseNormalView.hideLoadingDialog();
    }

    public void setLoadingText(String loadingText) {
        this.loadingText = loadingText;
    }

}
